package kr.couponplay.android.activity.recent;

import java.util.ArrayList;

import kr.couponplay.android.activity.R;
import kr.couponplay.android.activity.TitleBarHelper;
import kr.couponplay.android.activity.TitleBarHelper.TabMenuType;
import kr.couponplay.android.activity.TitleBarHelper.TitleLayout;
import kr.couponplay.android.activity.deal.list.DealListViewAdapter;
import kr.couponplay.android.domain.BeanFactory;
import kr.couponplay.android.domain.deal.Deal;
import kr.couponplay.android.domain.deal.DealDao;
import kr.couponplay.android.domain.deal.DealList;
import kr.couponplay.android.support.AlertUtils;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.LinearLayout;
import android.widget.ListView;

@SuppressLint("HandlerLeak") 
public class RecentActivity extends Activity implements OnItemClickListener {
	private static final TabMenuType TAB_MENU_TYPE = TabMenuType.RECENT;
	private ProgressDialog dataLoadingDialog;
	private ListView recentDealListView;
//	private LinearLayout dealListHeaderView;
	private DealListViewAdapter recentDealListAdapter;
	
	@Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.recent_activity);
        
        drawTop();
    	drawBody();
//        getDataFromServer();
    }
	
    @Override
    public void onResume() {
    	super.onResume();
    	TitleBarHelper.setVisibable(TAB_MENU_TYPE);
    	getDataFromServer();
    }
    
	private void drawTop() {
		LinearLayout titleLinearLayout = (LinearLayout)LayoutInflater.from(this).inflate(R.layout.recent_title, null);
		TitleBarHelper.addTitleLayout(new TitleLayout(titleLinearLayout, TAB_MENU_TYPE));
	}
	
	private void drawBody() {
		recentDealListView = (ListView) findViewById(R.id.recent_deal_list_view);
//		dealListHeaderView = (LinearLayout) getLayoutInflater().inflate(R.layout.deal_list_row_ad, null);
//		recentDealListView.addHeaderView(dealListHeaderView);
		
    	recentDealListAdapter = new DealListViewAdapter(this, R.layout.deal_list_row, new ArrayList<Deal>());
		recentDealListView.setAdapter(recentDealListAdapter);
		recentDealListView.setOnItemClickListener(this);
	}	
	
    private void getDataFromServer() {
    	dataLoadingDialog = ProgressDialog.show(this, null, "데이터 수신중입니다.", true, false);

        Thread thread = new Thread(new Runnable() {
            public void run() {
            	Message msg = Message.obtain();
            	try {
            		msg.obj = BeanFactory.getBean(getApplicationContext(), DealDao.class).selectRecentViewDeals(recentDealListAdapter.getCount());
            	} catch (Exception e) {
            		msg.obj = e;
            	}
            	handler.sendMessage(msg);
            }
        });
        thread.start();
    }
    
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
	    	if (msg.obj instanceof Throwable) {
	    		alert((Exception)msg.obj);
	    		dataLoadingDialog.dismiss();
	    		return;
	    	}
	    	DealList dealList = (DealList)msg.obj;
            drawDealListView(dealList);
            dataLoadingDialog.dismiss();
        }
    };
    
    public void alert(Exception exception) {
        AlertUtils.errorAlert(this, exception);
    }
    
	private void drawDealListView(DealList dealList) {
		recentDealListAdapter.clear();
		for (Deal each : dealList.getDeals()) {
			recentDealListAdapter.add(each);
		}
		recentDealListAdapter.notifyDataSetChanged();
	} 
    
	@Override
	public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
		Uri uri = Uri.parse(recentDealListAdapter.getItem(position).getDetailViewUrl());
		startActivity(new Intent(Intent.ACTION_VIEW, uri));
	}	
	
    @Override
    public void onBackPressed() {
    	TitleBarHelper.getTabHost().setCurrentTab(0);
    }
}
